// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "Sphere.h"

Sphere::Sphere(double radius, int spacing, const QString &name) :
    Item(name)
{
    if (90 % spacing != 0) {
        qDebug() << "90/spacing must be an integer. Setting to 10.";
        spacing = 10;
    }
    if (radius <= 0.0) {
        qDebug() << "radius must be positive";
        radius = 1.0;
    }

    /* FIXME complete the code */
    double delTheta = atan(spacing/radius);//M_PI*spacing/180.0;
    double delPhi   = delTheta;//M_PI*spacing/180.0;
    qDebug()<<delPhi;
    //return;

    // At the Nord pole
    for (double s = 0; s < 2*M_PI - delPhi; s+=delPhi){
        double sinT1 = sinf(0);
        double cosT1 = cosf(0);
        double sinP1= sinf(s);
        double cosP1= cosf(s);

        double sinT2 = sinf(delTheta);
        double cosT2 = cosf(delTheta);
        double sinP2= sinf(s+delPhi);
        double cosP2= cosf(s+delPhi);

        Vector P11( radius * cosP1 * sinT1, radius * sinP1 * sinT1, radius * cosT1);
        Vector P21( radius * cosP1 * sinT2, radius * sinP1 * sinT2, radius * cosT2);
        Vector P22( radius * cosP2 * sinT2, radius * sinP2 * sinT2, radius * cosT2);

        // Face
        Face F(Face::Triangles);

        // Add point to Face
        F.addPoint(P11,P11.normalized());
        F.addPoint(P21,P21.normalized());
        F.addPoint(P22,P22.normalized());

        //Set color to face
        F.setColor(QColor("green"));

        //Add face to item
        this->addFace(F);

    }

    //// At the South pole
    for (double s = 0; s < 2*M_PI - delPhi; s+=delPhi){
        double sinT1 = sinf(M_PI - delTheta);
        double cosT1 = cosf(M_PI - delTheta);
        double sinP1= sinf(s);
        double cosP1= cosf(s);

        double sinT2 = sinf(M_PI);
        double cosT2 = cosf(M_PI);
        double sinP2= sinf(s+delPhi);
        double cosP2= cosf(s+delPhi);

        Vector P11( radius * cosP1 * sinT1, radius * sinP1 * sinT1, radius * cosT1);
        Vector P12( radius * cosP2 * sinT1, radius * sinP2 * sinT1, radius * cosT1);
        Vector P22( radius * cosP2 * sinT2, radius * sinP2 * sinT2, radius * cosT2);

        // Face
        Face F(Face::Triangles);

        // Add point to Face
        F.addPoint(P11,P11.normalized());
        F.addPoint(P12,P12.normalized());
        F.addPoint(P22,P22.normalized());

        //Set color to face
        F.setColor(QColor("green"));

        //Add face to item
        this->addFace(F);

    }

    // The points arround
    for (double t = delTheta; t < M_PI - 2*delTheta; t+=delTheta){
        for (double s = 0.0; s < 2*M_PI - delPhi; s+=delPhi){
             double sinT1 = sinf(t);
             double cosT1 = cosf(t);
             double sinP1= sinf(s);
             double cosP1= cosf(s);

             double sinT2 = sinf(t+delTheta);
             double cosT2 = cosf(t+delTheta);
             double sinP2= sinf(s+delPhi);
             double cosP2= cosf(s+delPhi);

            //Coordonner des 4 point
            Vector P11( radius * cosP1 * sinT1, radius * sinP1 * sinT1, radius * cosT1);
            Vector P12( radius * cosP2 * sinT1, radius * sinP2 * sinT1, radius * cosT1);
            Vector P21( radius * cosP1 * sinT2, radius * sinP1 * sinT2, radius * cosT2);
            Vector P22( radius * cosP2 * sinT2, radius * sinP2 * sinT2, radius * cosT2);

            // Face
            Face F(Face::Quads);

            // Add point to Face
            F.addPoint(P11,P11.normalized());
            F.addPoint(P12,P12.normalized());
            F.addPoint(P21,P21.normalized());
            F.addPoint(P22,P22.normalized());

            //Set color to face
            F.setColor(QColor("green"));

            //Add face to item
            this->addFace(F);
        }
    }








    /* STOP here */
}

void Sphere::onFrameChanged(double frame)
{
    Q_UNUSED(frame);
    /* FIXME complete the code */
    // (optional)
    // apply a transformation matrix on this item using setGLTransform(
    Matrix m = Matrix::rotationY(frame/10);
    setTransform(m);
    // and frame to make the cube move.

    /* STOP here */
}
